//
//  HYHomeVideoCollectionCell.m
//  XPlayer
//
//  Created by liguoqing on 2023/9/5.
//

#import "HYHomeVideoCollectionCell.h"
#import <HYUtls/CommUtls+Time.h>

@interface HYHomeVideoCollectionCell ()

@property (nonatomic, strong) UIImageView *playLogo;

@property (nonatomic, strong) UIView *timeBackView;

@end

@implementation HYHomeVideoCollectionCell

- (void)configUI
{
    [super configUI];
    [self.backView addSubview:self.playLogo];
    [self.backView addSubview:self.timeBackView];
    [self.backView addSubview:self.descLabel];
    [self.backView addSubview:self.lockView];
    [self.playLogo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.coverView.mas_bottom).offset(-2);
        make.left.equalTo(self.coverView).offset(2);
        make.width.height.mas_equalTo(24);
    }];
    [self.timeBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backView).offset(kItemSpace+2);
        make.left.equalTo(self.backView).offset(kItemSpace+2);
        make.right.equalTo(self.descLabel.mas_right).offset(4);
        make.height.mas_equalTo(20);
    }];
    [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backView).offset(kItemSpace+2);
        make.left.equalTo(self.timeBackView.mas_left).offset(kItemSpace);
        make.height.mas_equalTo(20);
    }];
    [self.lockView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.backView);
        make.bottom.equalTo(self.backView.mas_top).offset(-15);
        make.width.height.mas_equalTo(44);
    }];
    [self addLongPressGesture];
}

- (void)bindViewModel:(HYHomeFileItemViewModel *)vm
{
    [super bindViewModel:vm];
    if(vm.isLock){
        self.iconView.hidden = NO;
        self.iconView.image = [UIImage imageNamed:@"icon_lock_mode"];
        self.coverView.image = [UIImage imageNamed:@""];
        self.coverView.backgroundColor = [CommUtls dynamicColorWithLightModeHex:@"#F5F5F5" darkModeHex:@"#404246"];
    }else{
        self.iconView.hidden = YES;
        if(vm.videoImage){
            self.coverView.image = vm.videoImage;
        }else{
            if(vm.fileImage){
                vm.videoImage = [UIImage imageWithContentsOfFile:[HYFileManager videoImageSavePathWithFileName:vm.fileImage]];
                self.coverView.image = vm.videoImage;
            }else{
                dispatch_async(dispatch_get_global_queue(0, 0), ^{
                    UIImage *coverImage = [CommUtls fetchVideoCoverImageWithUrl:vm.filePath atTime:1.0];
                    if(coverImage){
                        dispatch_async(dispatch_get_main_queue(), ^{
                            vm.videoImage = coverImage;
                            self.coverView.image = vm.videoImage;
                        });
                    }else{
                        dispatch_async(dispatch_get_main_queue(), ^{
                            self.iconView.hidden = NO;
                            self.coverView.image = nil;
                            self.coverView.backgroundColor = APPMainThemeColor;
                            self.iconView.image = [UIImage imageNamed:@"icon_video"];
                        });
                    };
                });
            }
        }
    }

    self.titleLabel.text = vm.fileName;
    self.descLabel.hidden = NO;
    if(vm.videoDuration){
        self.descLabel.text = vm.videoDuration;
    }else if(vm.fileSize){
        self.descLabel.text = [CommUtls getSize:@(vm.fileSize)];
    }else{
        self.descLabel.hidden = YES;
    }
    if(vm.importDate){
        self.dateLabel.text = vm.importDate;
    }
    [self.descLabel sizeToFit];
}

#pragma mark - loadLazy
- (UIImageView *)playLogo
{
    if(!_playLogo){
        _playLogo = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_play_light"]];
    }
    return _playLogo;
}

- (UIView *)timeBackView
{
    if(!_timeBackView){
        _timeBackView = [[UIView alloc]init];
        _timeBackView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        _timeBackView.layer.cornerRadius = 4.0;
        _timeBackView.layer.masksToBounds = YES;
    }
    return _timeBackView;;
}

@end
